﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BO;

namespace Site_CamaraTibau
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                try
                {
                   carregaNoticias();
                }
                catch (Exception)
                {
                    
                    throw;
                }
            }
        }

        public void carregaNoticias()
        {
            try
            {
                 var bco = new ModeloContainer();

                List<Noticia> noticias = new List<Noticia>();

                noticias = (from x in bco.Noticias where x.id == x.id orderby x.dtPublicacao descending select x).ToList();

                Noticia not = noticias.FirstOrDefault();

                if (not != null)
                {
                    imgCapa.ImageUrl = "~/fotos/noticia/" + not.id + "/" + FotoBO.SelecionarCapa(not.id).urlFoto;
                    lkbCapa.Text = not.titulo.Trim();
                    lkbCapa.PostBackUrl = "~/VerNoticia.aspx?ID=" + not.id.ToString();
                    noticias.Remove(not);
                }
                if (noticias.Count > 0)
                {
                    var objs = (from a in noticias
                                select new
                                {
                                    ID = a.id,
                                    urlCapa = "~/fotos/noticia/" + a.id + "/" + FotoBO.SelecionarCapa(a.id).urlFoto,
                                    titulo = a.titulo,
                                    categoria = a.Categoria.descricao,
                                    urlLink = "~/VerNoticia.aspx?ID=" + a.id,
                                    resumo = a.resumo,
                                    data = a.dtPublicacao.Value.Day.ToString().PadLeft(2, '0') + "/" + a.dtPublicacao.Value.Month.ToString().PadLeft(2, '0') + " " + a.dtPublicacao.Value.Hour.ToString().PadLeft(2, '0') + ":" + a.dtPublicacao.Value.Minute.ToString().PadLeft(2, '0'),
                                    autor = a.Usuario.nomeCompleto
                                }).ToList();

                    rptNoticias.DataSource = objs.Take(6);
                    rptNoticias.DataBind();


                    for (int j = 0; j < objs.Count; j++)
                    {
                        objs.Remove(objs[j]);
                    }

                    rptNoticiasBaixo.DataSource = objs;
                    rptNoticiasBaixo.DataBind();
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}